package com.sduept.nwld.dataserver.manager.cim;

import com.sduept.cim.ledger.total.model.ProtectionDeviceCount;
import com.sduept.core.dao.SuperDAO;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProtectionDeviceCountQueryManager {
	@Autowired
	private SuperDAO dao;

	/**
	 *	查询指定年份的调管单位统计结果
	 * @param year
	 * @return
	 */
	public List<ProtectionDeviceCount> getDispatchProtectionDeviceCount(int year) {
		return commonOperate(year, ProtectionDeviceCountPersistManager.dispatchType);
	}

	/**
	 * 	查询指定年份的检修单位统计结果
	 * @param year
	 * @return
	 */
	public List<ProtectionDeviceCount> getOvelProtectionDeviceCount(int year) {
		return commonOperate(year, ProtectionDeviceCountPersistManager.ovelType);
	}

	/**
	 * 	查询指定年份的生产厂家统计结果
	 * @param year
	 * @return
	 */
	public List<ProtectionDeviceCount> getManuProtectionDeviceCount(int year) {
		return commonOperate(year, ProtectionDeviceCountPersistManager.manuType);
	}

	/**
	 * 获取保护设备统计表中已存在的所有年份
	 */
	public List<Integer> getProtectCountYears() {
		String sql = "select distinct statisticTime from t_protection_device_count order by year desc";
		List<Integer> years = (List<Integer>) dao.nativeQuery(sql);
		return years;
	}

	/**
	 * @param year
	 * @param type
	 * @param voltage
	 * @return
	 */
	public List<ProtectionDeviceCount> getProtectionDeviceCountByYearTypeVoltage(int year, byte type, Integer voltage) {
		Date start = DateUtil.getDateByString(year + "-01-01 00:00:00", DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(year + "-12-31 23:59:59", DateUtil.pattern_ymdhms);
		return dao.findByCondition("unitType=? AND statisticTime BETWEEN ? AND ? AND voltageLevel = ?  ",
				new Object[] { type, start, end, voltage }, "ORDER BY unitName,voltageLevel", null,
				ProtectionDeviceCount.class);
	}
	/**按调管范围统计指定年份和电压等级
	 * @param year
	 * @param type
	 * @param voltage
	 * @return
	 */
	public List<ProtectionDeviceCount> getDispatchProtectionDeviceCountByYearTypeVoltage(int year, int voltage) {
		Date start = DateUtil.getDateByString(year + "-01-01 00:00:00", DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(year + "-12-31 23:59:59", DateUtil.pattern_ymdhms);
		return dao.findByCondition("unitType=0 AND statisticTime BETWEEN ? AND ? AND voltageLevel = ?  ",
				new Object[] { start, end, voltage }, "ORDER BY unitName,voltageLevel", null,
				ProtectionDeviceCount.class);
	}

	/**
	 * @param year
	 * @param type
	 * @param voltage 
	 * @return
	 */
	public List<ProtectionDeviceCount> getProtectionDeviceCountByYearTypeVoltage(int year, byte type,
			String[] voltage) {
		Date start = getStartTime(year);
		Date end = getEndTime(year);
		String insql = "";
		for (int i = 0; i < voltage.length; i++) {
			if (i == voltage.length - 1) {
				insql += "" + voltage[i] + "";
			} else {
				insql += "" + voltage[i] + ",";
			}
		}
		String esql = "unitType=? AND statisticTime BETWEEN ? AND ? AND voltageLevel IN (" + insql + ")";
		return dao.findByCondition(esql, new Object[] { type, start, end }, "ORDER BY unitName,voltageLevel", null,
				ProtectionDeviceCount.class);
	}

	/**
	 * @param year
	 * @param type
	 * @param voltage 
	 * @return
	 */
	public List<ProtectionDeviceCount> getProtectionDeviceCountByYearType(int year, byte type) {
		Date start = getStartTime(year);
		Date end = getEndTime(year);
		String esql = "unitType=? AND statisticTime BETWEEN ? AND ?";
		return dao.findByCondition(esql, new Object[] { type, start, end }, "ORDER BY unitName,voltageLevel", null,
				ProtectionDeviceCount.class);
	}

	/**
	 * 公共操作，根据年份，类型查询
	 * @param year
	 * @param type
	 * @return
	 */
	private List<ProtectionDeviceCount> commonOperate(int year, byte type) {
		Date start = DateUtil.getDateByString(year + "-01-01 00:00:00", DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(year + "-12-31 23:59:59", DateUtil.pattern_ymdhms);
		return dao.findByCondition("unitType=? AND statisticTime BETWEEN ? AND ? ", new Object[] { type, start, end },
				"ORDER BY unitName,voltageLevel", null, ProtectionDeviceCount.class);

	}

	/**
	 * 根据年份获得查询的起始时间
	 * @param year
	 * @return
	 */
	private Date getStartTime(int year) {
		return DateUtil.getDateByString(year + "-01-01", DateUtil.pattern_ymd);
	}

	/**
	 * 根据年份获得查询时间的结束时间
	 * @param year
	 * @return
	 */
	private Date getEndTime(int year) {
		return DateUtil.getDateByString((year + 1) + "-01-01", DateUtil.pattern_ymd);
	}

	/**
	 * @param year 年份
	 * @param type 单位类型（调度和运维） 0：调度单位 ；1：检修单位；2：运维单位，3：生产厂家
	 * @param voltage 电压等级
	 * @param unitName 单位名称
	 * @return
	 */
	public ProtectionDeviceCount getProtectionDeviceCountByYearTypeVoltageUnitName(int year, byte type, Integer voltage,
			String unitName, Byte statisticType) {
		Date start = DateUtil.getDateByString(year + "-01-01 00:00:00", DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(year + "-12-31 23:59:59", DateUtil.pattern_ymdhms);
		List<ProtectionDeviceCount> result = dao.findByCondition(
				"unitType=? AND statisticTime BETWEEN ? AND ? AND voltageLevel = ? AND unitName like ? AND statisticType = ? ",
				new Object[] { type, start, end, voltage, unitName, statisticType }, "ORDER BY unitName,voltageLevel",
				null, ProtectionDeviceCount.class);
		if (result.size() > 0) {
			return result.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 新增或更新ProtectionDeviceCount
	 * @param count
	 */
	public void createOrUpdateProtectionDeviceCount(ProtectionDeviceCount count) {
		dao.createOrUpdate(count, ProtectionDeviceCount.class);
	}

}
